// #include <unistd.h>
#include <time.h>
#include "cmsis_os.h"
#include "task.h"
#include "FreeRTOS.h"
#define MICROSECONDS_PER_SECOND    ( 1000000LL )                                   /**< Microseconds per second. */
#define NANOSECONDS_PER_SECOND     ( 1000000000LL )                                /**< Nanoseconds per second. */
#define NANOSECONDS_PER_TICK       ( NANOSECONDS_PER_SECOND / configTICK_RATE_HZ ) /**< Nanoseconds per FreeRTOS tick. */

// void UTILS_NanosecondsToTimespec( int64_t llSource,
//                                   struct timespec * const pxDestination )
// {
//     long lCarrySec = 0;
//
//     /* Convert to timespec. */
//     pxDestination->tv_sec = ( time_t ) ( llSource / NANOSECONDS_PER_SECOND );
//     pxDestination->tv_nsec = ( long ) ( llSource % NANOSECONDS_PER_SECOND );
//
//     /* Subtract from tv_sec if tv_nsec < 0. */
//     if( pxDestination->tv_nsec < 0L )
//     {
//         /* Compute the number of seconds to carry. */
//         lCarrySec = ( pxDestination->tv_nsec / ( long ) NANOSECONDS_PER_SECOND ) + 1L;
//
//         pxDestination->tv_sec -= ( time_t ) ( lCarrySec );
//         pxDestination->tv_nsec += lCarrySec * ( long ) NANOSECONDS_PER_SECOND;
//     }
// }

// int clock_gettime( int clock_id,struct timespec * tp )
// {
//
//     TimeOut_t xCurrentTime = { 0 };
//
//  /*Intermediate variable used to convert TimeOut_t to struct timespec.
//      * Also used to detect overflow issues. It must be unsigned because the
//      * behavior of signed integer overflow is undefined.
// */
//     uint64_t ullTickCount = 0ULL;
//
// /*
//  Silence warnings about unused parameters.
// */
//
//     ( void ) clock_id;
// /*
//
//  Get the current tick count and overflow count. vTaskSetTimeOutState()
//      * is used to get these values because they are both static in tasks.c.
// */
//
//     vTaskSetTimeOutState( &xCurrentTime );
//
// /*
//  Adjust the tick count for the number of times a TickType_t has overflowed.
//      * portMAX_DELAY should be the maximum value of a TickType_t.
// */
//
//     ullTickCount = ( uint64_t ) ( xCurrentTime.xOverflowCount ) << ( sizeof( TickType_t ) * 8 );
//
//  //Add the current tick count.
//
//     ullTickCount += xCurrentTime.xTimeOnEntering;
//
//  //Convert ullTickCount to timespec.
//
//     UTILS_NanosecondsToTimespec( ( int64_t ) ullTickCount * NANOSECONDS_PER_TICK, tp );
//     return 0;
// }
